package com.yuanqi.repository;

import com.yuanqi.domain.RetailCustomer;
import com.yuanqi.domain.CustomerType;
import com.yuanqi.domain.CustomerStatus;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Optional;

/**
 * 零售客户数据访问层
 */
@Repository
public interface RetailCustomerRepository extends JpaRepository<RetailCustomer, Long> {
    
    /**
     * 根据客户编码查找
     */
    Optional<RetailCustomer> findByCustomerCode(String customerCode);
    
    /**
     * 根据客户ID查找
     */
    Optional<RetailCustomer> findByCustomerId(Long customerId);
    
    /**
     * 根据客户类型查找
     */
    List<RetailCustomer> findByCustomerType(CustomerType customerType);
    
    /**
     * 根据会员等级查找
     */
    List<RetailCustomer> findByMemberLevel(String memberLevel);
    
    /**
     * 查找VIP客户
     */
    @Query("SELECT rc FROM RetailCustomer rc WHERE rc.customerType = 'VIP' AND rc.status = 'Active'")
    List<RetailCustomer> findVipCustomers();
    
    /**
     * 根据客户ID查找（替代客户姓名查询）
     */
    @Query("SELECT rc FROM RetailCustomer rc WHERE rc.customerId = :customerId AND rc.status = 'Active'")
    List<RetailCustomer> findByCustomerIdAndStatus(@Param("customerId") Long customerId);
    
    /**
     * 查找启用的零售客户
     */
    List<RetailCustomer> findByStatusOrderByCreatedAtDesc(CustomerStatus status);
}
